
import type { RequestConfig, ResponseData } from "./branch-test-table";
import BranchTestTable from "./branch-test-table";
import ky, { HTTPError, TimeoutError } from "ky";

const formData = new FormData()
formData.append('a', '2')
const successRequestConfig: RequestConfig[] = [
  { api: 'success/200', method: 'get', description: '普通get请求', search: { a: 1 } },
  { api: 'success/201', method: 'post', description: '普通post请求', search: { a: 2 }, json: { a: 2 } },
  { api: 'success/201', method: 'post', description: 'post发送application/x-www-form-urlencoded请求', /* search: { a: 2 }, */ body: new URLSearchParams({ a: '2' }) },
  { api: 'success/201', method: 'post', description: 'post发送multipart/form-data请求', /* search: { a: 2 }, */ body: formData },
  { api: 'success/202', method: 'put', description: '普通put请求' },
  { api: 'success/203', method: 'delete', description: '普通delete请求' },
  { api: 'success/204', method: 'patch', description: '普通patch请求' },
  { api: 'success/205', method: 'options', description: '普通options请求' },
  { api: 'success/206', method: 'head', description: '普通head请求' },
  { api: 'success/207', method: 'get', description: '普通get请求' },
  { api: 'success/208', method: 'post', description: '普通post请求' },
  { api: 'success/226', method: 'put', description: '普通put请求' },
]

const redirectRequestConfig: RequestConfig[] = [
  { api: 'redirect/300', method: 'get', description: '300重定向请求' },
  { api: 'redirect/301', method: 'get', description: '301重定向请求' },
  { api: 'redirect/302', method: 'get', description: '302重定向请求' },
  { api: 'redirect/303', method: 'get', description: '303重定向请求' },
  { api: 'redirect/304', method: 'get', description: '304重定向请求' },
  { api: 'redirect/307', method: 'get', description: '307重定向请求' },
  { api: 'redirect/308', method: 'get', description: '308重定向请求' },
]

const clientErrorRequestConfig: RequestConfig[] = [
  { api: 'client-error/400', method: 'get', description: '400响应' },
  { api: 'client-error/401', method: 'get', description: '401响应' },
  { api: 'client-error/402', method: 'get', description: '402响应' },
  { api: 'client-error/403', method: 'get', description: '403响应' },
  { api: 'client-error/404', method: 'get', description: '404响应' },
  { api: 'client-error/405', method: 'get', description: '405响应' },
  { api: 'client-error/406', method: 'get', description: '406响应' },
  { api: 'client-error/407', method: 'get', description: '407响应' },
  { api: 'client-error/408', method: 'get', description: '408响应' },
  { api: 'client-error/409', method: 'get', description: '409响应' },
  { api: 'client-error/410', method: 'get', description: '410响应' },
  { api: 'client-error/411', method: 'get', description: '411响应' },
  { api: 'client-error/412', method: 'get', description: '412响应' },
  { api: 'client-error/413', method: 'get', description: '413响应' },
  { api: 'client-error/414', method: 'get', description: '414响应' },
  { api: 'client-error/415', method: 'get', description: '415响应' },
  { api: 'client-error/416', method: 'get', description: '416响应' },
  { api: 'client-error/417', method: 'get', description: '417响应' },
  { api: 'client-error/418', method: 'get', description: '418响应' },
  { api: 'client-error/421', method: 'get', description: '421响应' },
  { api: 'client-error/422', method: 'get', description: '422响应' },
  { api: 'client-error/423', method: 'get', description: '423响应' },
  { api: 'client-error/424', method: 'get', description: '424响应' },
  { api: 'client-error/425', method: 'get', description: '425响应' },
  { api: 'client-error/426', method: 'get', description: '426响应' },
  { api: 'client-error/428', method: 'get', description: '428响应' },
  { api: 'client-error/429', method: 'get', description: '429响应' },
  { api: 'client-error/431', method: 'get', description: '431响应' },
  { api: 'client-error/451', method: 'get', description: '451响应' },
]

const serverErrorRequestConfig: RequestConfig[] = [
  { api: 'server-error/500', method: 'get', description: '500响应' },
  { api: 'server-error/501', method: 'get', description: '501响应' },
  { api: 'server-error/502', method: 'get', description: '502响应' },
  { api: 'server-error/503', method: 'get', description: '503响应' },
  { api: 'server-error/504', method: 'get', description: '504响应' },
  { api: 'server-error/505', method: 'get', description: '505响应' },
  { api: 'server-error/506', method: 'get', description: '506响应' },
  { api: 'server-error/507', method: 'get', description: '507响应' },
  { api: 'server-error/508', method: 'get', description: '508响应' },
  { api: 'server-error/509', method: 'get', description: '509响应' },
  { api: 'server-error/510', method: 'get', description: '510响应' },
  { api: 'server-error/511', method: 'get', description: '511响应' },
]

const timeoutRequestConfig: RequestConfig[] = [
  { api: 'timeout', method: 'get', description: '客户端超时', search: { timeout: 60 * 1000 } },
  { api: 'timeout', method: 'post', description: '客户端超时', search: { timeout: 60 * 1000 } },
  { api: 'timeout', method: 'put', description: '客户端超时', search: { timeout: 60 * 1000 } },
  { api: 'timeout', method: 'delete', description: '客户端超时', search: { timeout: 60 * 1000 } },
  { api: 'timeout', method: 'patch', description: '客户端超时', search: { timeout: 60 * 1000 } },
  { api: 'timeout', method: 'options', description: '客户端超时', search: { timeout: 60 * 1000 } },
  { api: 'timeout', method: 'head', description: '客户端超时', search: { timeout: 60 * 1000 } },
]

const apis: RequestConfig[] = [
  ...successRequestConfig,
  ...redirectRequestConfig,
  ...clientErrorRequestConfig,
  ...serverErrorRequestConfig,
  ...timeoutRequestConfig,
]
export const hasNoBodyResponseStatus = [204, 205, 206, 300,/* 301,302, */303, 304, 307,/* 308 */]

export default function FetcherTest() {

  return (
    <div>
      <h2 className="text-2xl font-bold mb-4">Ky</h2>
      <BranchTestTable apis={apis}
        call={async (params) => {
          // 模拟API调用，返回符合ResponseData接口的数据
          return ky(params.api, {
            retry: 1,
            method: params.method,
            headers: {
              Accept: 'application/json',
            },
            timeout: 10 * 1000,
            searchParams: params.search,
            body: params.body,
            // json: params.json,
            prefixUrl: '/api',
            hooks: {
              beforeError: [
                (error) => {
                  if (error instanceof TimeoutError) {
                    debugger
                    error.message = 'timeout'
                    return error
                  } else if (error instanceof HTTPError) {
                    const { response } = error
                    error.message = response.statusText
                    return error
                  }
                  return error
                }
              ]
            }
          }).then(
            resp => {
              if (hasNoBodyResponseStatus.includes(resp.status)) {
                const data: ResponseData = {
                  contentType: "",
                  serach: {},
                  body: null,
                  message: "no body response"
                }
                return data
              }
              return resp.json()
            },
            (error) => {
              if (error instanceof TimeoutError) {
                error.message = 'timeout'
                throw error
              }
              throw error
            })
        }} />


    </div>
  );
}